/*
 * 	<pre>
 *	EasyPeas.
 *  Copyright (C) 2007 S Owen
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Sep 25, 2007
 * ViewHandlerBean.java
 * </pre>
 */
package org.easypeas.http;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * A bean that always gets added to the property map, passed to the velocity
 * engine.
 * </p>
 * <p>
 * The purpose of this bean is to allow for other EasyPea based local paths to
 * be inserted into the current page, i.e useful for importing widgets.<br>
 * This is achieved by invoking {@link #read(String)} with the path. e.g. <br>
 * <code>
 * <div id='weather'>@viewhandler.read("widgets/weather/today")</div>
 * </code>
 * 
 * @author S Owen
 * @see EasyPeasHandler
 */
public class ViewHandlerBean {

	private static Logger logger = Logger.getLogger("org.easypeas.http.ViewHandlerBean");

	private final HttpServletRequest request;
	private final HttpServletResponse response;

	/**
	 * Instantiates a new view handler bean.
	 * 
	 * @param request
	 *            the request
	 * @param response
	 *            the response
	 */
	protected ViewHandlerBean(HttpServletRequest request,
			HttpServletResponse response) {
		super();
		this.request = request;
		this.response = response;
	}

	/**
	 * Processes the supplied path with
	 * {@link EasyPeasHandler#handleView(HttpServletRequest, HttpServletResponse, String)},
	 * using the request and response provided to the constructor. The effect of
	 * this is that the view generated by the supplied path gets inserted into
	 * the response stream.
	 * 
	 * @param path
	 *            to the local Easypea resource
	 */
	public void read(String path) {
		logger.info("In ViewHandler.read");
		try {
			EasyPeasHandler.getInstance().handleView(request, response, path);
		} catch (ServletException e) {
			logger.log(Level.SEVERE,"Error handling ",e);
		}
	}

}
